//
//  YCStockAlertView.m
//  lianganyuchu
//
//  Created by iMac on 17/3/2.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCStockAlertView.h"

@interface YCStockAlertView()
{
    UIView *m_backView;
    UIButton *m_btnCancel;
    UIButton *m_btnConfirm;
    UITextField *m_numField;
    UILabel *m_labNum;
    UILabel *m_labType;
    
    UILabel *m_labTitle;
}
@end
@implementation YCStockAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubviewsWith:frame];
    }
    return self;
}
-(void)addSubviewsWith:(CGRect)frame
{
    UIView *back=[[UIView alloc]init];
    [back setBackgroundColor:[UIColor blackColor]];
    [back setAlpha:0.2f];
    [back click:self action:@selector(clickBack:)];
    [self addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
    }];
    
    
    m_backView=[[UIView alloc]init];
    [m_backView setBackgroundColor:WHITE_COLOR];
    m_backView.layer.cornerRadius=5.0f;
    [self addSubview:m_backView];
    [m_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(280, 165));
        make.center.equalTo(back);
    }];
    
    CGFloat backW=280;
    CGFloat backH=165;
    
    m_labType=[[UILabel alloc]init];
    [m_labType setTextColor:TEXT_MIDDLE];
    [m_labType setFont:[UIFont LightFontOfSize:15]];
    [m_backView addSubview:m_labType];
    [m_labType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_backView).offset(kDistance);
        make.top.equalTo(m_backView).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(15);
    }];
    
    m_labTitle=[[UILabel alloc]init];
    [m_labTitle setTextColor:TEXT_DEEP];
    [m_labTitle setText:@"选择售卖份数"];
    [m_labTitle setFont:[UIFont LightFontOfSize:18]];
    [m_backView addSubview:m_labTitle];
    [m_labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_backView).offset(kDistance);
        make.top.equalTo(m_labType.bottom).offset(20);
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(20);
    }];
    
    m_labNum=[[UILabel alloc]init];
    [m_labNum setTextColor:TEXT_MIDDLE];
    [m_labNum setFont:[UIFont LightFontOfSize:15]];
    [m_labNum setAdjustsFontSizeToFitWidth:YES];
    [m_backView addSubview:m_labNum];
    [m_labNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(m_backView.right).offset(-kDistance);
        make.centerY.equalTo(m_labTitle.mas_centerY);
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(20);
    }];
    
    
    m_numField=[[UITextField alloc]init];
    [m_numField setTextColor:MAIN_COLOR];
    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kSmallDistance, 30)];
    m_numField.leftView=leftView;
    m_numField.leftViewMode=UITextFieldViewModeAlways;
    m_numField.delegate=self;
    m_numField.keyboardType=UIKeyboardTypeNumberPad;
    m_numField.returnKeyType=UIReturnKeyDone;
    [m_numField setFont:[UIFont LightFontOfSize:15]];
    m_numField.layer.cornerRadius=2.0f;
    m_numField.layer.borderWidth=1.0f;
    m_numField.layer.borderColor=LINE_COLOR.CGColor;
    [self addSubview:m_numField];
    [m_numField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(m_labNum.left).offset(-5);
        make.left.equalTo(m_labTitle.right).offset(40);
        make.centerY.mas_equalTo(m_labTitle.mas_centerY);
        make.height.mas_equalTo(30);
    }];

    
    m_btnCancel=[[UIButton alloc]init];
    [m_btnCancel setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [m_btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [m_btnCancel.titleLabel setFont:[UIFont LightFontOfSize:16]];
    [m_btnCancel addTarget:self action:@selector(btnCancelClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_backView addSubview:m_btnCancel];
    [m_btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_backView).offset(0);
        make.bottom.equalTo(m_backView).offset(0);
        make.width.mas_equalTo(backW/2);
        make.height.mas_equalTo(50);
    }];
    [m_btnCancel.layer addSublayer:getLine(0, backW/2, 0, 0, MAIN_COLOR)];
    
    m_btnConfirm=[[UIButton alloc]init];
    [m_btnConfirm setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [m_btnConfirm setTitle:@"确定" forState:UIControlStateNormal];
    [m_btnConfirm setBackgroundColor:MAIN_COLOR];
    [m_btnConfirm.titleLabel setFont:[UIFont LightFontOfSize:16]];
    [m_btnConfirm addTarget:self action:@selector(btnConfirmClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_backView addSubview:m_btnConfirm];
    [m_btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(m_backView).offset(0);
        make.bottom.equalTo(m_backView).offset(0);
        make.width.mas_equalTo(backW/2);
        make.height.mas_equalTo(50);
    }];
    [m_btnConfirm.layer addSublayer:getLine(0, backW/2, 0, 0, MAIN_COLOR)];
}
-(void)btnCancelClicked:(UIButton *)button
{
    [m_numField resignFirstResponder];
    [self removeFromSuperview];
    self.cancelBlock();
}
-(void)btnConfirmClicked:(UIButton *)button
{
    [m_numField resignFirstResponder];
    [self removeFromSuperview];
    if (_isSave) {
        self.saveBlock();
    }else{
        self.confirmBlock(m_numField.text);
    }
}
-(void)showAlertWith:(ItemTable *)item
{
    [m_labType setText:item.name];
    [m_labNum setText:[NSString stringWithFormat:@"/%@",item.num]];
    [m_numField setText:item.add_num];
    [m_numField becomeFirstResponder];
}

-(void)showSaveAlert:(NSString *)title
{
    _isSave=YES;
    [m_labType setHidden:YES];
    [m_numField setHidden:YES];
    [m_labNum setHidden:YES];
    [m_labTitle setText:@"是否保存排序？"];
    
    [m_labTitle setTextAlignment:NSTextAlignmentCenter];
    [m_btnCancel setTitle:@"不保存" forState:UIControlStateNormal];
    [m_btnConfirm setTitle:@"保存" forState:UIControlStateNormal];
    
    
    [m_backView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(280, 150));
        make.center.equalTo(self);
    }];
    
    [m_labTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_backView).offset(kDistance);
        make.right.equalTo(m_backView).offset(-kDistance);
        make.top.equalTo(m_labType.bottom).offset(5);
        make.height.mas_equalTo(20);
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)clickBack:(id)sender
{
    [m_numField resignFirstResponder];
}



@end






